package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.dto.SysDictDataDTO;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/admin/dict/data")
@Api(tags = "(运营端) 数据字典明细")
public class SysDictDataController {
    @Resource
    private ISysDictDataService dictDataService;

    @GetMapping("/list")
    @ApiOperation(value = "字典值列表查询",response = SysDictData.class)
    public ResultEntity list(SysDictDataDTO params) {
        PageUtil.startPage(params);
        return ResultEntity.createSuccess(PageUtil.getPageList(dictDataService.getDictDataList(params)));
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询字典数据信息",response = SysDictData.class)
    public ResultEntity getInfo(@PathVariable Long id) {
        return ResultEntity.createSuccess(dictDataService.getById(id));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增字典数据",response = SysDictData.class)
    public ResultEntity add(@Validated @RequestBody SysDictDataDTO params) {
        SysDictData entity = new SysDictData();
        BeanUtils.copyProperties(params, entity);
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        dictDataService.save(entity);
        return ResultEntity.createSuccess(entity);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改字典数据",response = SysDictData.class)
    public ResultEntity edit(@Validated @RequestBody SysDictDataDTO params) {
        SysDictData entity = new SysDictData();
        BeanUtils.copyProperties(params, entity);
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        dictDataService.updateById(entity);
        return ResultEntity.createSuccess(entity);
    }

    @PostMapping("/{dictIds}")
    @ApiOperation(value = "删除字典数据")
    public ResultEntity remove(@PathVariable String[] dictIds) {
        return ResultEntity.createSuccess(dictDataService.removeByIds(Arrays.asList(dictIds)));
    }
}
